package com.lbd.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.lbd.gulimall.product.entity.AttrEntity;
import com.lbd.gulimall.product.service.AttrAttrgroupRelationService;
import com.lbd.gulimall.product.service.AttrService;
import com.lbd.gulimall.product.service.CategoryService;
import com.lbd.gulimall.product.vo.AttrGroupWithAttrsVO;
import com.lbd.gulimall.product.vo.AttrRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lbd.gulimall.product.entity.AttrGroupEntity;
import com.lbd.gulimall.product.service.AttrGroupService;
import com.lbd.common.utils.PageUtils;
import com.lbd.common.utils.R;



/**
 * 属性分组
 *
 * @author linbd
 * @email 2236000738@qq.com
 * @date 2021-06-04 17:54:12
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @RequestMapping("/{catelogId}/withattr")
    public R selectGroupWithAttr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOS = attrGroupService.selectGroupWithAttr(catelogId);
        return R.ok().put("data",attrGroupWithAttrsVOS);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Integer catId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.getCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     *
     * @return:
     * @author: linbd
     * @date: 2021/6/17 20:19
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrRelationVO[] vos){
        attrAttrgroupRelationService.deleteAttrRelation(vos);
        return R.ok();
    }

    @RequestMapping("/{attrgroupId}/attr/relation")
    public R getRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrService.getRelation(attrgroupId);
        return R.ok().put("data",attrEntities);
    }

    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrRelationVO> vos) {
        attrAttrgroupRelationService.saveRelation(vos);
        return R.ok();
    }

    /**
     * 获取属性分组没有关联的其他属性
     *
     * @param :
     * @return:
     * @author: linbd
     * @date: 2021/6/17 22:46
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoAttrRelation(attrgroupId,params);
        return R.ok().put("page",page);
    }

}
